﻿Public Class ServerForm
    Friend WithEvents OutputListBox As New ListBox() With {.Dock = DockStyle.Fill, .IntegralHeight = False}

    Private Sub ServerForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Winsock1.Close()
    End Sub

    Private Sub ServerForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Controls.Add(OutputListBox)
        Winsock1.Listen(123)
    End Sub

    Private Sub Winsock1_DataReceived(sender As Object, e As NetWinsock.DataReceivedEventArgs) Handles Winsock1.DataReceived
        'Servers read requests received from clients and process them...
        Dim requestString As String = System.Text.Encoding.ASCII.GetString(e.ReceivedData.Data)
        OutputListBox.Items.Insert(0, e.ReceivedData.ClientInfo.Client.Client.RemoteEndPoint.ToString & ":> " & requestString)
        '(Implement processing...)

        'Then they write response data to the ClientInfo.Stream.
        Dim responseBytes() As Byte = System.Text.Encoding.ASCII.GetBytes("Data recieved: " & requestString)
        e.ReceivedData.ClientInfo.Stream.Write(responseBytes, 0, responseBytes.Length)
    End Sub

    Private Sub Winsock1_WinsockError(sender As Object, e As NetWinsock.WinsockErrorEventArgs) Handles Winsock1.WinsockError
        MessageBox.Show(e.Exception.Message)
    End Sub
End Class
